﻿using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.Rpc.Client;

namespace Wicture.EPC.Service.V1.Services.EPC.OnlineQuery
{
    public class EPCOnlineQueryService : RpcRepository
    {
        readonly string _serviceName = "Wicture.EPC.OnlineQueryProxy";
        //{hasError: false, errorMessage: '', data: {vin: task.params.vin, result01: rst, result02: rst01}}
        public JObject QueryVin(string vin, string brand)
        {
            try
            {
                var rpcResult = Call<JObject>(_serviceName, "Vin_Query", new { vin, brand });
                return rpcResult.data;
            }
            catch (LogicalException ex)
            {
                throw new Exception(ex.Message);
            }
            catch (Exception ex)
            {
                LoggerManager.Logger.LogError("EPCOnlineQueryService.QueryVin", ex);
                throw ex;
            }
        }

        //{hasError: false, errorMessage: '', data: {partCode: xxx, price: 0, cost: 1}}
        public JObject RealTimeQueryPartPrice(string partCode, string brand)
        {
            try
            {
                var rpcResult = Call<JObject>(_serviceName, "Price_Query", new { partCode, brand });
                return rpcResult.data;
            }
            catch (LogicalException ex)
            {
                throw new Exception(ex.Message);
            }

        }

        //{hasError: false, errorMessage: '', data: {partCode: xxx, price: 0, cost: 1}}
        public JObject RealTimeQueryPartPriceAndStock(string partCode, string brand)
        {
            try
            {
                var rpcResult = Call<JObject>(_serviceName, "Query_Price_And_Stock", new { partCode, brand });
                return rpcResult.data;
            }
            catch (LogicalException ex)
            {
                throw new Exception(ex.Message);
            }
            catch(Exception ex)
            {
                LoggerManager.Logger.LogError("EPCOnlineQueryService.RealTimeQueryPartPriceAndStock", ex);
                throw ex;
            }
        }
    }
}
